package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.AlgorithmPadding;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyWanBangYuan;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 万邦元信息咨询(上海)有限公司
 *
 * @author 李源聪
 */
@ApiPlan(productId = "351", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMaskWanBangYuan implements ApiOrgPhoneMaskConvertHandler {
    private static final Integer FILED_ID = 435;
    private static final String KEY = "GowJu6UkV96H0zj6";
    private static final String IV = "CSosjWbju3cybZaA";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        Map<Object, Object> newMap = new LinkedHashMap<>();
        newMap.put("phone", apiDataExchangeParameter.getPhoneMask());
        return JsonUtils.toJson(newMap);
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 200);
        if (!code) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        //  获取手机号掩码
        List<String> listPhoneMd5 = CollUtils.newArrayList(newList -> {
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            IteratorUtils.array(jsonArray, (protocolResult, index) -> {
                String phoneMd5  = jsonArray.getString(index);
                newList.add(phoneMd5);
            });
        });
        RespOrgPhoneMaskDataExchange respOrgPhoneMaskDataExchange = RespOrgPhoneMaskDataExchange.ofSuc();
        respOrgPhoneMaskDataExchange.setListPhoneMd5(listPhoneMd5);
        return respOrgPhoneMaskDataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyWanBangYuan orgApplyWanBangYuan = new OrgApplyWanBangYuan();
        //拓展
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //file_id
        orgApplyWanBangYuan.setFile_id(FILED_ID);
        //姓名
        AES cbc = AES.createCbc(KEY.getBytes(), IV.getBytes(), AlgorithmPadding.ZERO);
        String userName = cbc.encryptToBase64Str(apiApplyParameter.getUserName());
        orgApplyWanBangYuan.setName(userName);
        //手机号码
        String phone = cbc.encryptToBase64Str(apiApplyParameter.getPhone());
        orgApplyWanBangYuan.setMobile(phone);
        //年龄(若没有传0)
        orgApplyWanBangYuan.setAge(apiFieldConverter.getAgeInt());
        //性别(不传默认0。男传0、女传1)
        Integer newSex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 0)
                .when(apiApplyParameter.getSex(), "1", 1)
                .end(0);
        orgApplyWanBangYuan.setSex(newSex);
        //城市(如天津市)
        orgApplyWanBangYuan.setCity(apiApplyParameter.getCity());
        //是否有房(是传1,否传0)
        orgApplyWanBangYuan.setIs_house(apiFieldConverter.hasRealEstateInfoToNumber());
        //是否有车(是传1,否传0)
        orgApplyWanBangYuan.setIs_car(apiFieldConverter.hasCarInfoToNumber());
        //是否有公司营业执照(是传1,否传0)
        orgApplyWanBangYuan.setIs_company(apiFieldConverter.hasBizLicenseYearToNumber());
        //是否有信用卡 (是传1,否传0)
        orgApplyWanBangYuan.setIs_credit(apiFieldConverter.hasCreditLimitToNumber());
        //是否有保单(是传1,否传0)
        orgApplyWanBangYuan.setIs_insurance(apiFieldConverter.hasInsurancePolicyToNumber());
        //是否有社保(是传1,否传0)
        orgApplyWanBangYuan.setIs_social(apiFieldConverter.hasSocialSecurityToNumber());
        //是否有公积金(是传1,否传0)
        orgApplyWanBangYuan.setIs_fund(apiFieldConverter.hasProvidentFundToNumber());
        //是否有打卡工资(是传1,否传0)
        Integer newWork = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 0)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 0)
                .end(0);
        orgApplyWanBangYuan.setIs_work(newWork);
        //是否有营业税(是传1,否传0)
        orgApplyWanBangYuan.setIs_tax(0);
        //是否有微粒贷(是传1,否传0)
        orgApplyWanBangYuan.setWebank(0);
        //申请额度（单位:元）
        orgApplyWanBangYuan.setMoney_demand(apiFieldConverter.getLoanAmountInt());
        return JsonUtils.toJson(orgApplyWanBangYuan);
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 0);
        if (!code) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
